//
//  KeepCallsSelectCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/22.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "KeepCallsSelectCell.h"
#import "KeepCallCollectionCell.h"
#import "KeepCallsController.h"
@interface KeepCallsSelectCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionVeiw;
@property (weak, nonatomic) IBOutlet UILabel *leftLabel;

@property (weak, nonatomic) IBOutlet UILabel *leftTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *leftThreeLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightThreeLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *myCollectionH;

/** <#note#> */
//@property (nonatomic, strong) NSIndexPath *selectIndex;
@end

@implementation KeepCallsSelectCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.myCollectionVeiw registerNib:[UINib nibWithNibName:NSStringFromClass([KeepCallCollectionCell class]) bundle:nil] forCellWithReuseIdentifier:[KeepCallCollectionCell identifierStr]];
    
    self.myCollectionVeiw.backgroundColor = [UIColor whiteColor];
    
    self.myCollectionVeiw.delegate = self;
    
    self.myCollectionVeiw.dataSource = self;
    
//    self.myCollectionH.constant = 54.0 * 3.0 + 10;
    
    self.leftLabel.font = MYFONT(15);
    
    self.leftTwoLabel.font = MYFONT(22);
    
    self.leftThreeLabel.font = MYFONT(13);
    
    self.rightLabel.font = MYFONT(13);
    
    self.rightTwoLabel.font = MYFONT(22);
    
    self.rightThreeLabel.font = MYFONT(15);
    // Initialization code
}


- (void)setModel:(GoodsDetailModel *)model{
    
    _model = model;
    
    self.leftTwoLabel.text = _model.selectSkuModel.price;
    
    self.rightTwoLabel.text = _model.selectSkuModel.price_rmb;
    
    
    if ([self.model.tag isEqualToString:@"0"]) {
        
        self.model.tag = @"1";
        for (GoodsSpecListModel *temp in self.model.spec_list) {
            
            for (GoodsSpecSubModel *sub in temp.value) {
                sub.isSelect = @"0";
            }
            
            temp.value.firstObject.isSelect = @"1";
            
        }
        
      
    }
    
    
    
    
    
    
    NSInteger row = _model.spec_list.lastObject.value.count;
    
    if (row % 3 == 0) {
        row = row + 0;
    } else if (row % 3 == 1){
        row = row + 2;
    }else{
        
        row = row + 1;
        
    }
    
    self.myCollectionH.constant =  row / 3 * 54.0  + 10;
    
    
    [self.myCollectionVeiw reloadData];
    
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    if (self.model == 0) {
        return 0;
    } else {
         return 1;
    }
    
    
   
    
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.model == 0) {
        return 0;
    } else {
        return _model.spec_list.lastObject.value.count;
    }
    
    
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    KeepCallCollectionCell *temp = (KeepCallCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[KeepCallCollectionCell identifierStr] forIndexPath:indexPath];
    
    temp.subModel = self.model.spec_list[1].value[indexPath.item];

    
    return temp;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
      [self.viewController.view endEditing:YES];
    
    
    GoodsSpecListModel *temp = self.model.spec_list[1];
    
    for (GoodsSpecSubModel *sub in temp.value) {
        sub.isSelect = @"0";
    }
    
    GoodsSpecSubModel *select = self.model.spec_list[1].value[indexPath.item];
    
    select.isSelect = @"1";
    
//    [collectionView reloadData];
    
    
    
    
    NSMutableArray *arrayM = [NSMutableArray array];
    for (GoodsSpecListModel *temp in self.model.spec_list) {
        NSMutableString *strM = [NSMutableString string];
        for (GoodsSpecSubModel *sub in temp.value) {
            
            if ([sub.isSelect isEqualToString:@"1"]) {
                
                [strM appendString:sub.spec_id];
                
                [strM appendString:@":"];
                
                [strM appendString:sub.spec_value_id];
                
                [arrayM addObject:strM];
            }
        }
    }
    

    
    
    NSInteger i = 0;
    NSInteger j = arrayM.count;
    for (GoodsSkuListModel *skuModel in self.model.sku_list) {
        i = 0;
        
        for (NSString *str1 in arrayM) {
            
            if ([skuModel.attr_value_items_format containsString:str1]) {
                i++;
            }
            
        }
        
        if (i == j) {
            self.model.selectSkuModel = skuModel;
        }
        
    }
    
    
    
     KeepCallsController *tempvc = (KeepCallsController *)self.viewController;
    
    [tempvc.plainTableView reloadData];
    
    
//    NSLog(@"%@",self.model.selectSkuModel.attr_value_items);
   
    
    
//    [self.viewController.view endEditing:YES];
//    
//    [collectionView reloadData];
    
    
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}







@end
